//
//  TXSearchInputBox.m
//  QianYan
//
//  Created by jay on 2020/8/7.
//  Copyright © 2020 jay. All rights reserved.
//

#import "TXSearchInputBox.h"

//键盘ToolBar背景高
#define kKeyboardBarBgH                      44
//键盘ToolBar确定按钮宽
#define kKeyboardBarOkW                      50
//键盘ToolBar确定按钮高
#define kKeyboardBarOkH                      30
//键盘ToolBar确定按钮右间距
#define kKeyboardBarOkRightMargin            20

//键盘工具条按钮颜色
#define TX_COLOR_KEYBOARD_TOOLBAR_BUTTON  TX_COLOR_FROM_RGB(0x6E609E)

@interface TXSearchInputBox () <UITextFieldDelegate>

@property (nonatomic, strong) UIToolbar *keyboardToolbar;

@property (nonatomic, assign) CGFloat leftViewMargin;
@property (nonatomic, assign) CGFloat rightViewMargin;
@property (nonatomic, assign) CGFloat textLeftMargin;
@property (nonatomic, assign) CGFloat textRightMargin;

@end

@implementation TXSearchInputBox

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.leftViewMargin = 10;
        self.rightViewMargin = 10;
        self.textLeftMargin = 28;
        self.textRightMargin = 20;
        
        self.inputTF.hidden = NO;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
     leftViewMargin:(CGFloat )leftViewMargin
    rightViewMargin:(CGFloat )rightViewMargin
     textLeftMargin:(CGFloat )textLeftMargin
    textRightMargin:(CGFloat )textRightMargin
{
    self = [super initWithFrame:frame];
    if (self) {
        self.leftViewMargin = leftViewMargin;
        self.rightViewMargin = rightViewMargin;
        self.textLeftMargin = textLeftMargin;
        self.textRightMargin = textRightMargin;
        
        self.inputTF.hidden = NO;
    }
    return self;
}

-(void)setLeftViewIcon:(UIImage *)image {
    
    self.inputTF.leftViewMode = UITextFieldViewModeAlways;
    self.inputTF.leftView = [[UIImageView alloc] initWithImage:image];
}

-(void)setInputTextColor:(UIColor *)textColor textFont:(UIFont *)textFont {
    
    self.inputTF.textColor = textColor;
    self.inputTF.font = textFont;
}

-(void)setPlaceholderText:(NSString *)placeholderText placeholderColor:(UIColor *)placeholderColor {
    
    self.inputTF.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName:placeholderColor}];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [[[UIApplication sharedApplication].delegate window] endEditing:YES];

    if (textField.text.length > 0) {
        
        if (self.searchTextBlock) {
            self.searchTextBlock(textField.text);
        }
    }
    
    return YES;
}

#pragma mark - Getter

-(InputItemTextField *)inputTF {
    if (_inputTF == nil) {
        _inputTF = [[InputItemTextField alloc] initWithFrame:CGRectZero leftViewMargin:self.leftViewMargin rightViewMargin:self.rightViewMargin textLeftMargin:self.textLeftMargin textRightMargin:self.textRightMargin];
        _inputTF.delegate = self;
        _inputTF.backgroundColor = [UIColor clearColor];
        _inputTF.textAlignment = NSTextAlignmentLeft;
        _inputTF.borderStyle = UITextBorderStyleNone;
        _inputTF.keyboardType = UIKeyboardTypeDefault;
        _inputTF.returnKeyType = UIReturnKeySearch;
        _inputTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _inputTF.autocorrectionType = UITextAutocorrectionTypeDefault;
        _inputTF.autocapitalizationType = UITextAutocapitalizationTypeSentences;
        _inputTF.enablesReturnKeyAutomatically = YES;
        _inputTF.keyboardAppearance = UIKeyboardAppearanceDefault;
        [self addSubview:_inputTF];
        
        _inputTF.inputAccessoryView = self.keyboardToolbar;

        [_inputTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    return _inputTF;
}

-(UIToolbar *)keyboardToolbar {
    if (_keyboardToolbar == nil) {
        _keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, TX_SCREEN_WIDTH, kKeyboardBarBgH)];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(TX_SCREEN_WIDTH-kKeyboardBarOkRightMargin-kKeyboardBarOkW, (kKeyboardBarBgH-kKeyboardBarOkH)/2, kKeyboardBarOkW, kKeyboardBarOkH)];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:TX_COLOR_KEYBOARD_TOOLBAR_BUTTON forState:UIControlStateNormal];
        
        [button bk_addEventHandler:^(id  _Nonnull sender) {
            
            [self.inputTF resignFirstResponder];
            
        } forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *btnTtem = [[UIBarButtonItem alloc] initWithCustomView:button];
        UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil];
        NSArray *items = @[flexibleItem,flexibleItem,btnTtem];
        [_keyboardToolbar setItems:items animated:YES];
    }
    return _keyboardToolbar;
}

@end
